{extend name="../../base/view/common/base" /}
{block name="body"}
<form class="layui-form p-4">
	{eq name="id" value ="0"}
	<h3 class="pb-3">新建文档</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">文档名称<font>*</font></td>
			<td>
				<input type="text" name="title" lay-verify="required" lay-reqText="请输入文档名称" placeholder="请输入文档名称" class="layui-input" value="">
			</td>
			<td class="layui-td-gray">关联项目<font>*</font></td>
			<td>
				<select id="projectId" name="project_id" lay-filter="project_id">
					<option value="">请选择关联项目</option>
					{volist name=":get_project()" id="v"}
					<option value="{$v.id}" {eq name="$v.id" value="$project_id"} selected{/eq}>{$v.name}</option>
					{/volist}
				</select>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray" style="vertical-align:top;">文档内容<font>*</font></td>
			<td colspan="5">
			  <textarea name="content" placeholder="请输入内容" class="layui-textarea" id="container" style="border:0;padding:0"></textarea>
			</td>
		</tr>
	</table>
	</div>
	{else/}
	<h3  class="pb-3">编辑文档</h3>
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">文档名称<font>*</font>
			</td>
			<td>
				<input type="text" name="title" lay-verify="required" lay-reqText="请输入文档名称" placeholder="请输入文档名称"
					class="layui-input" value="{$detail.title}">
			</td>
			<td class="layui-td-gray">关联项目<font>*</font></td>
			<td>
				<select id="projectId" name="project_id" lay-filter="project_id">
					<option value="">请选择关联项目</option>
					{volist name=":get_project()" id="v"}
					<option value="{$v.id}" {eq name = "$v.id" value="$detail.project_id"} selected{/eq}>{$v.name}</option>
					{/volist}
				</select>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray" style="vertical-align:top;">文档内容<font>*</font></td>
			<td colspan="5">
			  <textarea name="content" placeholder="请输入内容" class="layui-textarea" id="container" style="border:0;padding:0">{$detail.content}</textarea>
			</td>
		</tr>
	</table>
	{/eq}
	<div style="padding: 10px 0">
		<input type="hidden" name="id" value="{$id}" />
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool','tinymce'];
	function gouguInit() {
		var form = layui.form,tool=layui.tool;
		
		//编辑器初始化
		var editor = layui.tinymce;
		var edit = editor.render({
			selector: "#container",
			images_upload_url: '/api/index/upload/sourse/tinymce',//图片上传接口
			height: 500
		});

		//监听提交
		form.on('submit(webform)', function (data) {
		  data.field.content = tinyMCE.editors['container'].getContent();
			if (data.field.content == '') {
				layer.msg('请先完善文档内容');
				return false;
			}
			let callback = function (e) {
				layer.msg(e.msg);
				parent.layui.documentTable.reload();
				if (e.code == 0) {
					tool.sideClose(1000);
				}
			}
			tool.post("/project/document/add", data.field, callback);
			return false;
		});
  }

</script>
{/block}